package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.mapper.backend.MerchantBackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CooperativeMerchantMapper;
import com.ruoyi.system.domain.CooperativeMerchant;
import com.ruoyi.system.service.ICooperativeMerchantService;

/**
 * 合作商户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class CooperativeMerchantServiceImpl extends CxBase implements ICooperativeMerchantService
{
    @Autowired
    private CooperativeMerchantMapper cooperativeMerchantMapper;

    @Autowired
    private MerchantBackMapper merchantBackMapper;
    /**
     * 查询合作商户
     * 
     * @param merchantId 合作商户ID
     * @return 合作商户
     */
    @Override
    public CooperativeMerchant selectCooperativeMerchantById(String merchantId)
    {
        if(!checkNull(merchantId)){
            CooperativeMerchant cooperativeMerchant =cooperativeMerchantMapper.selectCooperativeMerchantById(merchantId);
            cooperativeMerchant.setSetDate(StampToString(Long.valueOf(cooperativeMerchant.getSetDate())));
           return cooperativeMerchant;
        }
        return null;
    }

    /**
     * 查询合作商户列表
     * 
     * @param cooperativeMerchant 合作商户
     * @return 合作商户
     */
    @Override
    public List<CooperativeMerchant> selectCooperativeMerchantList(CooperativeMerchant cooperativeMerchant)
    {
        startPage();
        List<CooperativeMerchant> cooperativeMerchants =cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
        for(CooperativeMerchant cooperativeMerchant1 : cooperativeMerchants){
            cooperativeMerchant1.setSetDate(StampToString(Long.valueOf(cooperativeMerchant1.getSetDate())));
        }
        return cooperativeMerchants;
    }

    /**
     * 新增合作商户
     * 
     * @param cooperativeMerchant 合作商户
     * @return 结果
     */
    @Override
    public int insertCooperativeMerchant(CooperativeMerchant cooperativeMerchant)
    {
        if(!checkNull(cooperativeMerchant.getMerchantName())&&!checkNull(cooperativeMerchant.getMerchantAddress())&&!checkNull(cooperativeMerchant.getSetId())){
            cooperativeMerchant.setMerchantId(getIdStamp().toString());
            cooperativeMerchant.setSetDate(getStamp().toString());
            if(checkNull(cooperativeMerchant.getMerchantPassword())){
                cooperativeMerchant.setMerchantPassword("MER"+cooperativeMerchant.getMerchantId());
            }
            return cooperativeMerchantMapper.insertCooperativeMerchant(cooperativeMerchant);
        }
        return 0;
    }

    /**
     * 修改合作商户
     * 
     * @param cooperativeMerchant 合作商户
     * @return 结果
     */
    @Override
    public int updateCooperativeMerchant(CooperativeMerchant cooperativeMerchant)
    {
        if(!checkNull(cooperativeMerchant.getMerchantId())){
            if(!checkNull(cooperativeMerchant.getMerchantPassword())) {
                List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
                if (cooperativeMerchants.size() < 1) {
                    return cooperativeMerchantMapper.updateCooperativeMerchant(cooperativeMerchant);
                }
            }
            cooperativeMerchant.setSetDate(getStamp().toString());
            return cooperativeMerchantMapper.updateCooperativeMerchant(cooperativeMerchant);
        }
        return 0;
    }

    /**
     * 批量删除合作商户
     * 
     * @param merchantIds 需要删除的合作商户ID
     * @return 结果
     */
    @Override
    public int deleteCooperativeMerchantByIds(String[] merchantIds)
    {
        if(merchantIds.length>0){
            merchantBackMapper.deleteAdvertisementMerchantByMerchantId(merchantIds);
            merchantBackMapper.deletePrizeMerchantByMerchantId(merchantIds);
            return cooperativeMerchantMapper.deleteCooperativeMerchantByIds(merchantIds);
        }
        return 0;
    }

    /**
     * 删除合作商户信息
     *
     * @param merchantId 合作商户ID
     * @return 结果
     */
    @Override
    public int deleteCooperativeMerchantById(String merchantId)
    {
        return cooperativeMerchantMapper.deleteCooperativeMerchantById(merchantId);
    }
}
